/**
 * 标签页组件
 */
import React from 'react';
import { Tabs } from 'antd';
import { isEmpty } from '../common';

import { getUITabs } from '../uiConfig';
import UIIcon from './UIIcon';

export default class UITabs extends React.Component {

    constructor(props) {
        super(props);

        let tabConfig = getUITabs(props.tabConfigId);

        this.state = {
            loading: false,
            data: null,
            tabConfig: tabConfig,
            tabKey: isEmpty(props.defaultActiveKey) ? tabConfig.tabPageList[0].name : props.defaultActiveKey,
        };
    }

    getParent = () => {
        return this.props.parent;
    }

    onTabChange = (activeKey) => {
        this.setState({ tabKey: activeKey });

        setTimeout(() => {

            if (this.props.onTabChange) {
                this.props.onTabChange(activeKey);
            }
        });
    }

    render() {

        const tabContentMap = this.props.tabContentMap;

        const tabPanelList = this.state.tabConfig.tabPageList.map((tab) => {
            return (
                <Tabs.TabPane tab={<span>{isEmpty(tab.icon) === false ? <UIIcon icon={tab.icon} /> : ''}{tab.label}</span>} key={tab.name}>
                    <div style={{ paddingLeft: 15 }}>
                        {
                            tabContentMap[tab.compentclass].getComponent(this, this.props)
                        }
                    </div>
                </Tabs.TabPane>
            );
        });

        const defaultActiveKey = this.props.defaultActiveKey !== undefined ? this.props.defaultActiveKey : this.state.tabKey;

        return (

            <Tabs onChange={this.onTabChange} tabBarExtraContent={this.props.tabBarExtraContent} type={this.props.type} size={this.props.size} tabPosition={this.props.tabPosition} defaultActiveKey={defaultActiveKey}>
                {tabPanelList}
            </Tabs>

        );
    }
}